package com.comunity_cloud.client.view2;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;

import net.iharder.dnd.FileDrop;

import com.comunity_cloud.client.controller.MainController2;
import com.comunity_cloud.client.model.CommunityFile;
import com.comunity_cloud.client.model.CommunityModel2;
import com.user_hosted_storage.dto.CommunityDTO;

public class CommunityPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5545261042737826673L;

	public CommunityPanel(final CommunityDTO communityDTO) {
		
		
		final JTable table = new JTable();
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBackground(Color.white);
		final CommunityModel2 communityModel = new CommunityModel2(communityDTO);
		table.setModel(communityModel);

		TableColumn fileColumn = table.getColumn("File");
		fileColumn.setPreferredWidth(220);
		fileColumn.setCellRenderer(new FileTableCellRenderer());
		
		TableColumn statusColumn = table.getColumn("Status");
		statusColumn.setPreferredWidth(110);
		statusColumn.setResizable(false);
		statusColumn.setCellRenderer(new FileTableCellRenderer());
		
		TableColumn sizeColumn = table.getColumn("Size");
		sizeColumn.setWidth(50);
		sizeColumn.setPreferredWidth(70);
		sizeColumn.sizeWidthToFit();
		
		statusColumn.setCellRenderer(new ProgressCellRenderer());
		table.setShowVerticalLines(false);
		table.setShowHorizontalLines(false);
		
//		table.setIntercellSpacing(new Dimension(0, 10));
		table.setRowHeight(table.getRowHeight()+10);
//		table.setBorder(new EmptyBorder(0, 10, 0, 10));
		
		setBackground(Color.white);
		scrollPane.getViewport().setBackground(Color.WHITE);
		setLayout(new BorderLayout());
		setBackground(Color.white);
		
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				table.repaint();
			}
			
			@Override
			public void mouseClicked(MouseEvent e) {
				if(e.getClickCount() == 2) {
					MainController2.getInstance().openFile( communityModel.getRow(table.getSelectedRow()));
				}
			}
		});
		
		JPopupMenu popupMenu = new JPopupMenu();
		
		JMenuItem deleteItem = new JMenuItem("Delete");
		popupMenu.add(deleteItem);
		table.setComponentPopupMenu(popupMenu);
		deleteItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				int[] selectedRows = table.getSelectedRows();
				LinkedList<CommunityFile> filesToDelete = new LinkedList<>();
				for (int i : selectedRows) {
					CommunityFile removeRow = communityModel.getRow(i);
					filesToDelete.add(removeRow);
				}
				for (CommunityFile removeRow : filesToDelete) {
					communityModel.communityFileDeleted(removeRow);
					MainController2.getInstance().deleteFile(removeRow);
				}
			}
		});
		
		JMenuItem propertiesItem = new JMenuItem("Properties");
		
		popupMenu.add(propertiesItem);
		propertiesItem.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				CommunityFile file = communityModel.getRow(table.getSelectedRow());
				
				final JFrame frame = new JFrame(file.getPath() + " - properties");
				
				CommunityFilePane communityFilePane = new CommunityFilePane(file);
				
				frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				
				frame.add(communityFilePane,BorderLayout.CENTER);
				
				
				
				JPanel buttonsPane = new JPanel();
				JButton okBtn = new JButton("OK");
				
				buttonsPane.add(okBtn);
				frame.add(buttonsPane,BorderLayout.SOUTH);
				okBtn.addActionListener(new ActionListener() {
					
					@Override
					public void actionPerformed(ActionEvent e) {
						frame.dispose();
					}
				});
				
				frame.pack();
//				frame.setIconImage(new ImageIcon(file.getImage()).getImage());
				frame.setVisible(true);
				
			}
		});
		
		new FileDrop( System.out, this, /*dragBorder,*/ new FileDrop.Listener()
		{   public void filesDropped( java.io.File[] files )
		{
			MainController2.getInstance().filesDropped(files,communityDTO);
		}   // end filesDropped
		}); // end FileDrop.Listener
		add(scrollPane);
	}


}
